package com.itheima.controller;

import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckitemController {

    @Autowired
    private CheckitemService checkitemService;

    //分页条件查询
    @PostMapping("/findPage")//每页10个
    public PageResult findPages(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkitemService.findbyPages(queryPageBean);
        return pageResult;
    }
    //新增检查项
    @PostMapping
    public Result saveCheckitem(@RequestBody CheckItem checkItem){
        checkitemService.saveCheckitem(checkItem);
        return Result.success("新增检查成功");
    }
    //删除检查项
    @DeleteMapping("/{id}")
    public Result deleteCheckitem(@PathVariable Integer id){
        Integer integer = checkitemService.deleteCheckitem(id);
        if(integer == 0){
            return Result.error("当前检查项已经被关联到检查组，不允许删除");
        }else
        return Result.success("删除检查项成功");
    }
    //根据id查询检查项
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        CheckItem checkItem = checkitemService.selectById(id);
        return Result.success(checkItem,"查询检查项成功");
    }
    //编辑检查项
    @PutMapping
    public Result updateCheckitem(@RequestBody CheckItem checkItem){
        checkitemService.updateByid(checkItem);
        return Result.success("编辑检查项成功");
    }
    //查询所有检查项数据【给下面检查组功能使用的】
    @GetMapping("/list")
    public Result selectAll(){
        List<CheckItem> results = checkitemService.selectAll();
        return Result.success(results, "查询检查项成功");
    }


}